package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.picture.IPictureView;
import liuliu.throughwaring.control.picture.PictureListener;
import liuliu.custom.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.model.PersonModel;

/**
 * Created by liuliu on 2015/11/18   18:24
 * 单位详细信息
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class UnitDetailActivity extends BaseActivity {
    @CodeNote(id = R.id.unit_detail_name)
    TextView unit_detail_name;//单位名称
    @CodeNote(id = R.id.unit_detail_type)
    TextView unit_detail_type;//单位类型
    @CodeNote(id = R.id.unit_detail_phone)
    TextView unit_detail_phone;//联系电话
    @CodeNote(id = R.id.unit_detail_Legal)
    TextView unit_detail_Legal;//法人
    @CodeNote(id = R.id.unit_detail_Security)
    TextView unit_detail_Security;//治安
    @CodeNote(id = R.id.unit_detail_address)
    TextView unit_detail_address;//单位地址
    @CodeNote(id = R.id.unit_detail_cratetime)
    TextView unit_detail_cratetime;//创建时间
    @CodeNote(id = R.id.unit_detail_remark)
    TextView unit_detail_remark;//备注
    @CodeNote(id = R.id.unit_detail_tel)
    Button unit_detail_tel;//拨打电话
    CompanyModel companyModel;
    @CodeNote(id = R.id.unit_detail_image)
    ImageView unit_detail_image;
    @CodeNote(id = R.id.unit_detail_toolbar)
    TToolbar unit_detail_toolbar;
    @CodeNote(id = R.id.unit_detail_changetime)
    TextView changetiem;
    @CodeNote(id = R.id.unit_detail_num)
    TextView tnum;
    @CodeNote(id = R.id.unit_detail_ll_num)
    LinearLayout llNum;
    List<CodeModel> mCodeList;

    @Override
    public void initViews() {
        setContentView(R.layout.activity_unit_detail);
        mCodeList = finalDb.findAllByWhere(CodeModel.class, "codeName='Code_CompanyType'");
        Intent intent = getIntent();
        companyModel = (CompanyModel) intent.getSerializableExtra("CompanyModel");
        setModel();
    }

    private String getCodeyCompanyValue(String value) {
        for (int i = 0; i < mCodeList.size(); i++) {
            if (value.equals(mCodeList.get(i).getName())) {
                return mCodeList.get(i).getValue();//流动人员
            }
        }
        return "";
    }

    private void setModel() {
        if (companyModel.getCompanyType().equals("Lease")) {
            unit_detail_toolbar.setCenterTitle("房屋详细信息");
        }
        unit_detail_name.setText(Utils.URLEncode(companyModel.getCompanyName()));

        unit_detail_remark.setText(Utils.URLEncode(companyModel.getCompanyComment()));
        if (getCodeyCompanyValue(companyModel.getCompanyType()).length() > 0) {
            unit_detail_type.setText(Utils.URLEncode(getCodeyCompanyValue(companyModel.getCompanyType()).substring(0, 2)));
        } else {
            unit_detail_type.setText("");
        }
        unit_detail_phone.setText(Utils.URLEncode(companyModel.getCompanyPhoneNumber()));
        unit_detail_Legal.setText(Utils.URLEncode(companyModel.getCompanyLegalPerson()));
        unit_detail_Security.setText(Utils.URLEncode(companyModel.getCompanySecurityPerson()));
        unit_detail_address.setText(Utils.URLEncode(companyModel.getCompanyAddress()));
        unit_detail_cratetime.setText(Utils.URLEncode(companyModel.getCompanyCreateTime()));
        changetiem.setText(Utils.URLEncode(companyModel.getCompanyChangeTime()));

        List<PersonModel> plist = finalDb.findAllByWhere(PersonModel.class, " PersonCompanyId='" + companyModel.getCompanyId() + "'");
        if (plist != null) {
            tnum.setText(plist.size() + "人");
        }
        llNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initEvents() {
        unit_detail_toolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
        unit_detail_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //用intent启动拨打电话
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + companyModel.getCompanyPhoneNumber()));
                startActivity(intent);
            }
        });
        Bitmap bitmap = Utils.getLoacalBitmap(companyModel.getCompanyOveralImageUrl());
        if (bitmap != null) {
            unit_detail_image.setImageBitmap(Utils.centerSquareScaleBitmap(bitmap, 60));
        }
    }
}
